package com.dlc.felear.xiaoensale.utils;

import android.os.Environment;

import com.itdlc.android.library.utils.FileUtil;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;

public class LogToFile {

    private String filePath= Environment.getExternalStorageDirectory().getAbsolutePath() + "/ZHDZ/";
    private ScheduledExecutorService sExecutor;
    private static  LogToFile instance;
    private String currentFile;

    private LogToFile() {
        createDir("ZHDZ");
        if(!FileUtil.checkFileIsExist(filePath+TimeUtils.getCurrrentTimeYMD()+".txt")){
            new File(filePath,TimeUtils.getCurrrentTimeYMD()+".txt");
        }
        currentFile=filePath+TimeUtils.getCurrrentTimeYMD()+".txt";
    }

    public static File createDir(String dirName) {
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + dirName);
        if (file.exists()) {
            return file;
        } else {
            file.mkdirs();
            return file;
        }
    }


    public static LogToFile getInstance() {
        if(instance==null){
            synchronized (LogToFile.class){
                if(instance==null){
                    instance=new LogToFile();
                }
            }
        }
        return instance;
    }

    public boolean input2File(final String input) {
        if (sExecutor == null) {
            sExecutor = Executors.newScheduledThreadPool(5);
        }
        Future<Boolean> submit = sExecutor.submit(new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                BufferedWriter bw = null;
                try {
                    if(!FileUtil.checkFileIsExist(currentFile)){
                        new File(currentFile);
                    }
                    // 构造给定文件名的FileWriter对象，并使用布尔值指示是否追加写入的数据。
                    FileWriter fileWriter = new FileWriter(currentFile, true);
                    bw = new BufferedWriter(fileWriter);
                    bw.write(input);
                    return true;
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                } finally {
                    try {
                        if (bw != null) {
                            bw.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        try {
            return submit.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return false;
    }
}
